package org.dslmagic.holder;

import java.util.Map;

import org.dslmagic.dsl.Dsl;
import org.dslmagic.util.Code;

public class ObjectHolder<T> {

	private final T object;

	public ObjectHolder(T object) {
		this.object = object;
	}

	public T getObject() {
		return object;
	}

	public <IT> T def(Class<IT> cinterface, Dsl dsl) {
		return def(cinterface, null, new Dsl[] { dsl });
	}

	public <IT> T def(Class<IT> cinterface, String methodName, Dsl dsl) {
		return def(cinterface, new String[] { methodName }, new Dsl[] { dsl });
	}

	@SuppressWarnings("unchecked")
	public <IT> T def(Class<IT> cinterface, String[] methodNames, Dsl[] dsls) {
		Map<String, Dsl> implementations = Code.implementations(cinterface,
				methodNames, dsls);
		return (T) Code.define(object, cinterface, implementations);
	}

	public <IT> IT as(Class<IT> cinterface, Dsl dsl) {
		return as(cinterface, null, new Dsl[] { dsl });
	}

	public <IT> IT as(Class<IT> cinterface, String methodName, Dsl dsl) {
		return as(cinterface, new String[] { methodName }, new Dsl[] { dsl });
	}

	@SuppressWarnings("unchecked")
	public <IT> IT as(Class<IT> cinterface, String[] methodNames, Dsl[] dsls) {
		Map<String, Dsl> implementations = Code.implementations(cinterface,
				methodNames, dsls);
		return (IT) Code.define(object, cinterface, implementations);
	}

	public <IT> T def(String methodName, Dsl dsl) {
		return def(null, new String[] { methodName }, new Dsl[] { dsl });
	}

	public <IT> T defAll(String methodNamePattern, Dsl dsl) {
		// Pattern pattern = Pattern.compile(methodNamePattern);
		throw new RuntimeException("This feature is currently not supported");
	}

}
